package com.ea.eadp.nexus.test.pageDefinition.originX;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;

import com.ea.eadp.nexus.test.common.PageOperation;
import com.ea.eadp.nexus.test.common.TestUtil;
import com.ea.eadp.nexus.test.pageDefinition.BasePage;


public class SigninPage extends BasePage {

    private static final String defaultUri = baseUri + TestUtil.SigninOriginXConnectURI();

    private static final String SIGNIN_BUTTON_ID = "logInBtn";
    private static final String FAIL_TO_LOG_IN_ERROR_ID = "online-general-error";

    private static final String FAIL_TO_LOG_IN_ERROR_MESSAGE = "Your Email, ID or Password is incorrect. Please try again.";

    public SigninPage(final WebDriver driver) {
        super(driver, SigninPage.class);
    }

    @Override
    public void navigate() {
        navigate(defaultUri);
    }

    @Override
    public void navigate(String uri) {
        driver.switchTo().window(operationWindowHandle);
        log.info(uri);
        driver.navigate().to(uri);
    }

    public void waitPSPageLoadCompleted() {

    }

    public void inputEmail(final String email) {
        PageOperation.SendKeyToElementAfterWait(driver, "email", email);
    }

    public void inputPassword(final String password) {
        PageOperation.SendKeyToElementAfterWait(driver, "password", password);
    }

    public void showPassword(final Boolean show) {

    }

    public void rememberMe(final Boolean checked) {
        driver.findElement(By.xpath("//form[@id='login-form']/div[6]/span[1]/label")).click();
    }

    public void signInAsInvisible(final Boolean checked) {

    }

    public void validateFailToLogin() {
        PageOperation.WebDriverWait(driver, FAIL_TO_LOG_IN_ERROR_ID);
        Assert.assertEquals(driver.findElement(By.id(FAIL_TO_LOG_IN_ERROR_ID)).getText(), FAIL_TO_LOG_IN_ERROR_MESSAGE);
    }

    public void clickSignInButton() {
        driver.switchTo().window(operationWindowHandle);
        driver.findElement(By.id(SIGNIN_BUTTON_ID)).click();
    }

    public void validateSignInButtonIsDisabled() {
        Assert.assertTrue(driver.findElement(By.id(SIGNIN_BUTTON_ID)).getAttribute("class").contains("disabled") || !driver.findElement(By.id(SIGNIN_BUTTON_ID)).isEnabled());
    }
}
